package jbstoremgmt.service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.HandlerInterceptor;

public class TestInterceptor implements HandlerInterceptor {
  
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		System.out.println("In pre-processing/n");
		System.out.println(request.getRequestURI());
		System.out.println(request.getQueryString());

		// Check if the request is for login page & the session exists.
		if(request.getRequestURI().equalsIgnoreCase("/jbstoremgmt/loginform.htm"))
		{	
			if(null !=request.getSession(false))
			{
				response.sendRedirect("loginsuccess.htm");
				return false;
			}
			return true;
			
			//System.out.println(request.getSession(false).getId());
		}
		if(null==request.getSession(false))
		{
			response.sendRedirect("loginform.htm");		
		     return false;
		}
		return true;
	}
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
		System.out.println("done with request/n");
	} 
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {   
		System.out.println("In post-processing/n");

	}
}